package com.mtiiot.fsmanage.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 16-8-1.
 */
public class TimeUtil {
    public static long[] handleTimes(String times) throws Exception{
        if(times==null){
            return null;
        }
        if("全部".equals(times)){
            return null;
        }
        if(times.equals("")){
            return null;
        }
        long start=0;
        long end=0;
        if("今天".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        } else if("本周".equals(times)){
            Calendar cal = Calendar.getInstance();
            int week=cal.get(Calendar.DAY_OF_WEEK);//得到是星期几
            cal.add(Calendar.DAY_OF_MONTH, 2-week);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("本月".equals(times)){
            Calendar cal = Calendar.getInstance();
            int day=cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 1-day);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("本年度".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("昨天".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);

            end=cal.getTimeInMillis();
        }else if("这个月".equals(times)){
            Calendar cal = Calendar.getInstance();
            int year=cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 1-year);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("前天".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -2);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -2);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("上周这一天".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -7);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -7);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("前一周".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -7);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("前一个月".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -1);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("前一年".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.YEAR, -1);
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.YEAR, -1);
            cal.set(Calendar.MONTH, 11);
            cal.set(Calendar.DAY_OF_MONTH, 31);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("最近15分钟".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MINUTE, -15);//分钟
            start=cal.getTimeInMillis();
            end=System.currentTimeMillis();

        }else if("最近30分钟".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MINUTE, -30);//分钟
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最近1小时".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -1);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最近4小时".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -4);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最近12小时".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -12);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最近24小时".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -24);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的7天".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -6);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的30天".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -29);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的60天".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -59);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的90天".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -89);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的6个月".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -179);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的1年".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -364);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的2年".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -729);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("最后的5年".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1824);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if(times.split("~")!=null){
            String[] tmp=times.split("~");
            if(tmp.length>0){
                String ts=tmp[0];
                String te=tmp[1];
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date d1=sdf.parse(ts);
                Date d2=sdf.parse(te);
                start=d1.getTime();
                end=d2.getTime();
            }else{
                return null;
            }
        }else {
            System.out.println("=============没有匹配任何时间=============");
            return null;
        }
        long[] l=new long[2];
        l[0]=start;
        l[1]=end;
        return l;
    }

    public static long getTimestampByOffsetDay(int day){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    public static void main(String[] args){
        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.HOUR_OF_DAY, -24);//小时
       // cal.add(Calendar.MINUTE, -30);//分钟
//        cal.add(Calendar.YEAR, -1);
//        cal.set(Calendar.MONTH, 0);
//        cal.set(Calendar.DAY_OF_MONTH, 1);
//        cal.set(Calendar.HOUR_OF_DAY, 0);//小时
//        cal.set(Calendar.MINUTE, 0);
//        cal.set(Calendar.SECOND, 0);
//        cal.set(Calendar.MILLISECOND, 0);
//        cal.add(Calendar.YEAR, -1);
//        cal.set(Calendar.MONTH, 11);
//        cal.set(Calendar.DAY_OF_MONTH, 31);
//        cal.set(Calendar.HOUR_OF_DAY, 23);//小时
//        cal.set(Calendar.MINUTE, 59);
//        cal.set(Calendar.SECOND, 59);
//        cal.set(Calendar.MILLISECOND, 999);
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);//小时
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        System.out.println(cal.get(Calendar.YEAR));
        System.out.println(cal.get(Calendar.MONTH)+1);
        System.out.println(cal.get(Calendar.DAY_OF_MONTH));
        System.out.println(cal.get(Calendar.HOUR_OF_DAY));
        System.out.println(cal.get(Calendar.MINUTE));
        System.out.println(cal.get(Calendar.SECOND));

//        Calendar cal = Calendar.getInstance();
//        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置周一为一星期第一天
//        cal.set(Calendar.HOUR_OF_DAY, 0);
//        cal.set(Calendar.SECOND, 0);
//        cal.set(Calendar.MINUTE, 0);
//        cal.set(Calendar.MILLISECOND, 001);
//        Sys.out.println(cal.get(Calendar.YEAR));
//        Sys.out.println(cal.getTime().getTime());
//        Sys.out.println(cal.getTimeInMillis());
//        Sys.out.println(new Date().getTime());

    }
}
